<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp" %>
<%@include file="/common/javascript.jsp"%>
<%@ taglib prefix="fns" uri="http://www.wistron.com/tag/fns"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>用户</title>
    <script type="text/javascript">
    var mainform;
    var viewType = "${viewType}";
        $(function ()
        {        
            mainform=$("#mainform");
            
            mainform.ligerForm();
            
            if(viewType=="modify"){
            	 $("#userId").attr("readonly", "readonly").removeAttr("validate");
            }
            
            if(viewType=="detail"){
            	$("input,select,textarea",mainform).attr("disabled", "disabled");
            }else{
                $.metadata.setType("attr", "validate");
                LG.validate(mainform);
            }
            
            LG.setFormDefaultBtn(cancel,viewType=="detail" ? null : save);
        });  
        
        
        function save(){
        	$("#roleIds").val(getSelectedRoleIds());
            LG.submitForm(mainform, function (result) {
            	if(typeof(result)=="string"){
            		result=eval("("+result+")");
            	}
                if (!result.success) {
                    LG.showError('错误:' + data.message);
                }
                else { 
                    LG.showSuccess('保存成功', function () { 
                       LG.callParent("userList");
                    });
                }
            });
        }
        
        function cancel(){
            var win = window.parent || window;
            if(win.dialog){
            	win.dialog.close();
            }
        }
        
        function selectRole()
        {
        	dialog=$.ligerDialog.open({title: '选择角色',showMax: true, showToggle: true, showMin: true, isResize: true, modal: true,name:'winselector',width:600, height: 300, url: '${ctx}/role/toSelect'});
        }

        function containsRoleId(roleId){
        	var options=getSelectRoles();
        	for(var i=0;i<options.length;i++){
        		if($(options[i]).val()==roleId){
        			return true;
        		}
        	}
        	return false;
        }
        function callBackOK(rows){
        	 var row;
             for(var i=0;i<rows.length;i++){
             	row=rows[i];
             	if(!containsRoleId(row.roleId)){
                 	$("#roleNames").append("<option value='"+row.roleId+"'>"+row.roleName+"</option>");
             	}
             }
             dialog.close();
        }
        
        function getSelectedRoleIds(){
        	var options=getSelectRoles();
        	var idArr=[];
        	for(var i=0;i<options.length;i++){
        		idArr.push($(options[i]).val());
        	}
        	return idArr.join(",");
        }
        
        function getSelectRoles(){
        	return $("#roleNames option");
        }
        
        function removeOption(){
        	 $("#roleNames option:selected").each(function(){
        		 $(this).remove();
        	 });
        }
    </script>
</head>

<body style="padding:10px">
    <form name="mainform" method="post"  id="mainform" action="${ctx }/user/save">
        <input type="hidden" name="viewType" value="${viewType}"/>
        <input type="hidden" id="usertype" name="usertype" value="${command.usertype}"/>
        <table cellpadding="0" cellspacing="0" class="l-table-edit" style="width: 100%;">
            <tr>
                <td align="left" class="l-table-edit-td">账号:</td>
                <td align="left" class="l-table-edit-td" style="width:160px" >
                	<input type="text"  name="userId" id="userId" value="${command.userId }" ltype="text" validate="{required:true,minlength:3,maxlength:50,notnull:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">启用:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	<c:choose>
                		<c:when test="${command.isEnable eq 'F' }">
                			<input id="isEnable" type="checkbox" name="isEnable" value="F" onchange="changeTnf(this)" <c:if test="${viewType=='detail'}">disabled="disabled"</c:if>/> 
                		</c:when>
                		<c:otherwise>
                			<input id="isEnable" type="checkbox" name="isEnable" value="T" checked="checked"  onchange="changeTnf(this)" <c:if test="${viewType=='detail'}">disabled="disabled"</c:if>/> 
                		</c:otherwise>
                	</c:choose>

                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">密码:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	<input type="password" name="userPassword" value="${command.userPassword }" id="userPassword" ltype="text" validate="{required:true,minlength:5,maxlength:20,notnull:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">有效期:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                <c:if test="${viewType!='detail'}">
                	<input type="text" name="dateExpire" id="dateExpire" value="${command.dateExpire }" ltype="date" validate="{required:true}" nullText="不能为空!"/>
                </c:if>
                <c:if test="${viewType=='detail'}">
                	<input type="text" value="${command.dateExpire }" />
                </c:if>	
                </td>
                <td align="left"></td>
            </tr>   
            
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">确认密码:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	<input type="password" name="confirmPassword" id="confirmPassword" value="${command.userPassword }" ltype="text" validate="{required:true,minlength:5,maxlength:20,notnull:true,equalTo:'#userPassword'}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">登陆日期:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	${command.dateLogin }
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">中文姓名:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	<input type="text" name="userNameC" id="userNameC" value="${command.userNameC }"  ltype="text" validate="{required:true,minlength:1,maxlength:10,notnull:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">登陆IP:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	${command.ipLogin }
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">英文姓名:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	<input type="text" name="userNameE" id="userNameE"  value="${command.userNameE }" ltype="text" validate="{required:true,minlength:1,maxlength:20,notnull:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top"></td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                </td>
                <td align="left"></td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td" valign="top">邮箱:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                	<input type="text" name="userEmail" id="userEmail" value="${command.userEmail }" ltype="text" validate="{required:true,notnull:true,email:true}" nullText="不能为空!"/>
                </td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top"></td>
                <td align="left" class="l-table-edit-td" style="width:160px">
                </td>
                <td align="left"></td>
            </tr>
            <tr>
            	<td align="left" class="l-table-edit-td" valign="top">角色:</td>
            	<td align="left" class="l-table-edit-td" style="width:160px" colspan="5">
            	<select id="roleNames" name="roleNames" multiple="multiple" style="width:200px;">
            		<c:forEach items="${command.roles}" var="role">
            			<option value="${role.roleId}">${role.roleName }</option>
            		</c:forEach>
            	</select>
            	<c:if test="${viewType!='detail'}">
       			<input type="hidden" id="roleIds"  name="roleIds"/>
       				<a onclick="removeOption()" href="javascript:;">移除</a>
       				<a onclick="selectRole()" href="javascript:;">选择</a>
       			</c:if>
            	</td>
            </tr>
            <tr>
                <td align="left" class="l-table-edit-td">备注:</td>
                <td align="left" class="l-table-edit-td"> 
                <textarea cols="100" rows="3" class="l-textarea" id="remark" name="remark" style="width:400px">${command.remark }</textarea>
                </td> <td align="left"></td>
           </tr>
           <tr>
                <td align="left" class="l-table-edit-td" valign="top">建档人:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">${command.userCre }</td>
                <td align="left"></td>
                
                <td align="left" class="l-table-edit-td" valign="top">修改人:</td>
                <td align="left" class="l-table-edit-td" style="width:160px">${command.userUpd}</td>
                <td align="left"></td>
           </tr>
        </table>
    </form>
</body>
</html>
